package com.yuyou.fn.settlement.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.github.wens.mybatisplus.toolkit.IdWorker;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.settlement.service.IMainAccountService;
import com.yuyou.fn.settlement.service.ISettlementService;
import com.yuyou.fn.settlement.vo.FeeQueryVo;
import com.yuyou.fn.settlement.vo.FeeSaveVo;
import org.springframework.stereotype.Service;

import com.yuyou.fn.settlement.mapper.FeeMapper;
import com.yuyou.fn.settlement.entity.Fee;
import com.yuyou.fn.settlement.service.IFeeService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 *
 * Fee 表数据服务层接口实现类
 *
 */
@Service
public class FeeServiceImpl extends SuperServiceImpl<FeeMapper, Fee,Long> implements IFeeService {

    @Resource
    private ISettlementService settlementService ;

    @Resource
    private IMainAccountService mainAccountService;


    @Override
    public Page<Fee> findFeeByQuery(FeeQueryVo feeQueryVo, int pageNo, int pageSize) {
        Example<Fee> example = Fee.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,feeQueryVo);
        return findPageByExample(example,pageNo,pageSize );
    }

    @Override
    public void saveFee(FeeSaveVo feeSaveVo) {
        Fee fee = new Fee() ;
        BeanUtils.copyProperties(fee,feeSaveVo);
        fee.setCreateTime(TimeUtils.now());
        fee.setUpdateTime(TimeUtils.now());
        fee.setTrackingId(IdWorker.getId());
        fee.setReceiverMainAccountName(mainAccountService.findById(fee.getReceiverMainAccountId()).getName());
        insertSelective(fee);
    }

    @Transactional
    @Override
    public void auditingFee(Long feeId, Integer status) {

        Fee updateFee = new Fee();
        updateFee.setFeeId(feeId);
        updateFee.setStatus(status);

        updateSelectiveById(updateFee);

        if(status == 1 ){//审核通过
            Fee fee = findById(feeId);
            settlementService.bookFee( fee.getMainAccountId(),fee.getReceiverMainAccountId(),fee.getAmount() ,fee.getRemark() , fee.getTags()  , fee.getTrackingId() );
        }

    }
}